package com.sqkj.controller;

import com.sqkj.entity.Comment;
import com.sqkj.entity.User;
import com.sqkj.service.CommentService;
import com.sqkj.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.xml.ws.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Comment")
public class CommentController {
    @Resource
    CommentService service;
    @Resource
    UserService userService;
    @RequestMapping("/findByArticleId")
    public Map<String, Object> findByArticleId(int articleId) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Comment> commentList = service.findByArticleId(articleId);
        map.put("commentList", commentList);
        return map;
    }
     @RequestMapping("/add")
    public Map<String, Object> add(String wxName , int articleId , String comment) {
         Map<String, Object> map = new HashMap<>();
         String insertTime = UpLoadController.dateformat.format(System.currentTimeMillis());
            User user = userService.findByWxName(wxName);
            Comment cot = new Comment(user.getId(), articleId, comment, insertTime);
            List<Comment> commentList = service.addComment(cot);
             map.put("commentList", commentList);
             return map;
     }
    @RequestMapping("/add2")
    public Map<String, Object> add2(String openId , int articleId , String comment) {
        Map<String, Object> map = new HashMap<>();
        String insertTime = UpLoadController.dateformat.format(System.currentTimeMillis());
        User user = userService.findByOpenId(openId);
        Comment cot = new Comment(user.getId(), articleId, comment, insertTime);
        List<Comment> commentList = service.addComment(cot);
        map.put("commentList", commentList);
        return map;
    }
}
